package com.yc.service;

import com.google.gson.Gson;
import com.yc.dimain.MessageBean;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zwei
 * @create 2024-08-11 19:07
 */
@Component
@Slf4j
@Transactional
public class JmsMessageConsumer {
    @Autowired
    private EmailBiz mailBiz;
    @Autowired
    private VelocityTemplateBizImpl velocityTemplateBiz;
//    @Autowired
//    private EmailWebsocket emailWebsocket;


    @JmsListener(destination = "好吃点餐网")    //监听的队列  当有消息发送到这个队列  就会执行这个方法
    public void receiveMessage(String message) {
        log.info("用户接收到的信息:" + message);
        Gson gson = new Gson();
        MessageBean mb = gson.fromJson(message, MessageBean.class);
        //产生要发生的邮件内容
        String context = velocityTemplateBiz.getEmailContext(mb.getOptype(), mb.getUsername(), mb.getMoney(),mb.getEmail());
        log.info("发送的内容:" + context);
        Document doc = Jsoup.parse(context);
        String text = doc.text();
        log.info("发送的内容:" + text);
        try {
            mailBiz.sendMail(mb.getEmail(), "好吃点餐网通知", context);
//            emailWebsocket.send(message);     //发送websocket消息
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
